package com.sport.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.sport.entity.RetailCustomer;
import com.sport.model.PageParams;
import com.sport.model.Result;
import com.sport.service.ProductService;
import com.sport.service.RetailCustomerService;
import com.sport.utils.ObjectUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "用户标签")
@RequestMapping("/customers")
@ApiSupport(author = "开心摆烂", order = 8)
public class RetailCustomerController {

    @Autowired
    private RetailCustomerService retailCustomerService;

    @Autowired
    private ProductService productService;

    /**
     * 获取客户信息分页列表
     *
     * @return 客户信息分页列表
     */
    @PostMapping("/page")
    @Operation(summary = "获取客户信息分页列表")
    @ApiOperationSupport(order = 1)
    public Result<Page<RetailCustomer>> getCustomerPage(@RequestBody PageParams<RetailCustomer> pageParams) {
        RetailCustomer params = pageParams.getParams();
        LambdaQueryWrapper<RetailCustomer> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotBlank(params.getCustId()), RetailCustomer::getCustId, params.getCustId());
        lqw.like(StrUtil.isNotBlank(params.getCustName()), RetailCustomer::getCustName, params.getCustName());
        Page<RetailCustomer> page = retailCustomerService.page(pageParams.getPage(), lqw);
        try {
            ObjectUtils.removeNullFieldsFromList(page.getRecords());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return Result.success(page);
    }


    /**
     * 获取客户信息列表
     *
     * @return 客户信息列表
     */
    @PostMapping("/list")
    @Operation(summary = "获取客户信息列表")
    @ApiOperationSupport(order = 2)
    public Result<List<RetailCustomer>> getCustomerList(@RequestBody RetailCustomer retailCustomer) {
        LambdaQueryWrapper<RetailCustomer> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotBlank(retailCustomer.getCustId()), RetailCustomer::getCustId, retailCustomer.getCustId());
        lqw.like(StrUtil.isNotBlank(retailCustomer.getCustName()), RetailCustomer::getCustName, retailCustomer.getCustName());
        List<RetailCustomer> customerList = retailCustomerService.list(lqw);
        try {
            List<RetailCustomer> filteredList = ObjectUtils.removeNullFieldsFromList(customerList);
            return Result.success(filteredList);
        } catch (IllegalAccessException e) {
            // 处理异常
            e.printStackTrace();
            return Result.error();
        }
    }



    /**
     * 根据客户ID获取客户详细信息
     *
     * @param custId 客户ID
     * @return 客户详细信息
     */
    @GetMapping("/{custId}")
    @Operation(summary = "根据客户ID获取客户详细信息")
    @ApiOperationSupport(order = 3)
    public Result<RetailCustomer> getCustomerById(
            @Parameter(description = "客户ID", required = true, in = ParameterIn.PATH) @PathVariable String custId) {
        RetailCustomer customer = retailCustomerService.getCustomerById(custId);
        if (customer != null) {
            return Result.success(customer);
        } else {
            return Result.error();
        }
    }




}
